﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.WarehouseManagement;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Controllers.WarehouseManagement
{
    /// <summary>
    /// 产品入库控制器     ---tsh
    /// </summary>
    [Route("WarehouseManagement/ProductsInbound")]
    public class ProductsInboundController : Controller
    {
        private readonly IProductsInboundServices _productsInboundServices;

        public ProductsInboundController(IProductsInboundServices productsInboundServices)
        {
            _productsInboundServices = productsInboundServices;
        }

        /// <summary>
        /// 产品入库管理页面
        /// </summary>
        /// <returns></returns>
        [HttpGet("ProductsInboundView")]
        public IActionResult ProductsInboundView()
        {
            return View("~/Views/WarehouseManagement/ProductsInboundView.cshtml");
        }

        /// <summary>
        /// 获取产品入库列表
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns></returns>
        [HttpPost("List")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> List(QueryProductsInboundDto input)
        {
            try
            {
                var result = await _productsInboundServices.GetListAsync(input);
                return Json(new 
                { 
                    code = 0, 
                    msg = "success", 
                    count = result.TotalCount, 
                    data = result.Items 
                });
            }
            catch (Exception ex)
            {
                return Json(new 
                { 
                    code = 1, 
                    msg = "获取列表失败：" + ex.Message,
                    count = 0,
                    data = new List<ProductsInboundDto>()
                });
            }
        }

        /// <summary>
        /// 根据ID获取产品入库详情
        /// </summary>
        /// <param name="id">产品入库ID</param>
        /// <returns></returns>
        [HttpGet("GetById")]
        public async Task<IActionResult> GetById(int id)
        {
            try
            {
                var result = await _productsInboundServices.GetByIdAsync(id);
                if (result != null)
                {
                    return Json(new 
                    { 
                        code = 0, 
                        msg = "获取成功", 
                        data = result 
                    });
                }
                else
                {
                    return Json(new 
                    { 
                        code = 1, 
                        msg = "未找到指定记录" 
                    });
                }
            }
            catch (Exception ex)
            {
                return Json(new 
                { 
                    code = 1, 
                    msg = "获取详情失败：" + ex.Message 
                });
            }
        }

        /// <summary>
        /// 删除产品入库记录
        /// </summary>
        /// <param name="id">产品入库ID</param>
        /// <returns></returns>
        [HttpPost("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                await _productsInboundServices.DeleteAsync(id);
                return Json(new 
                { 
                    code = 0, 
                    msg = "删除成功" 
                });
            }
            catch (Exception ex)
            {
                return Json(new 
                { 
                    code = 1, 
                    msg = "删除失败：" + ex.Message 
                });
            }
        }

        /// <summary>
        /// 批量删除产品入库记录
        /// </summary>
        /// <param name="ids">产品入库ID列表</param>
        /// <returns></returns>
        [HttpPost("BatchDelete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> BatchDelete([FromForm] List<int> ids)
        {
            try
            {
                if (ids == null || ids.Count == 0)
                {
                    return Json(new 
                    { 
                        code = 1, 
                        msg = "请选择要删除的记录" 
                    });
                }

                await _productsInboundServices.BatchDeleteAsync(ids);
                return Json(new 
                { 
                    code = 0, 
                    msg = $"批量删除成功，共删除 {ids.Count} 条记录" 
                });
            }
            catch (Exception ex)
            {
                return Json(new 
                { 
                    code = 1, 
                    msg = "批量删除失败：" + ex.Message 
                });
            }
        }
    }
}
